home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C26 / ExtractUndeliverable.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  2.0 KB  |  79 lines

  1. //: C26:ExtractUndeliverable.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Find undeliverable names to remove from 
  7. // mailing list from within a mail file 
  8. // containing many messages
  9. #include "../require.h"
  10. #include <cstdio>
  11. #include <string>
  12. #include <set>
  13. using namespace std;
  14.  
  15. char* start_str[] = {
  16.   "following address",
  17.   "following recipient",
  18.   "following destination",
  19.   "undeliverable to the following",
  20.   "following invalid",
  21. };
  22.  
  23. char* continue_str[] = {
  24.   "Message-ID",
  25.   "Please reply to",
  26. };
  27.  
  28. // The in() function allows you to check whether
  29. // a string in this set is part of your argument.
  30. class StringSet {
  31.   char** ss;
  32.   int sz;
  33. public:
  34.   StringSet(char** sa, int sza):ss(sa),sz(sza) {}
  35.   bool in(char* s) {
  36.     for(int i = 0; i < sz; i++)
  37.       if (strstr(s, ss[i]) != 0)
  38.         return true;
  39.     return false;
  40.   }
  41. };
  42.  
  43. // Calculate array length:
  44. #define ALEN(A) ((sizeof A)/(sizeof *A))
  45.  
  46. StringSet 
  47.   starts(start_str, ALEN(start_str)),
  48.   continues(continue_str, ALEN(continue_str));
  49.  
  50. int main(int argc, char* argv[]) {
  51.   requireArgs(argc, 2, 
  52.     "Usage:ExtractUndeliverable infile outfile");
  53.   FILE* infile = fopen(argv[1], "rb");
  54.   FILE* outfile = fopen(argv[2], "w");
  55.   require(infile != 0); require(outfile != 0);
  56.   set<string> names;
  57.   const int sz = 1024;
  58.   char buf[sz];
  59.   while(fgets(buf, sz, infile) != 0) {
  60.     if(starts.in(buf)) {
  61.       puts(buf);
  62.       while(fgets(buf, sz, infile) != 0) {
  63.         if(continues.in(buf)) continue;
  64.         if(strstr(buf, "---") != 0) break;
  65.         const char* delimiters= " \t<>():;,\n\"";
  66.         char* name = strtok(buf, delimiters);
  67.         while(name != 0) {
  68.           if(strstr(name, "@") != 0)
  69.             names.insert(string(name));
  70.           name = strtok(0, delimiters);
  71.         }
  72.       }
  73.     }
  74.   }
  75.   set<string>::iterator i = names.begin();
  76.   while(i != names.end())
  77.     fprintf(outfile, "%s\n", (*i++).c_str());
  78. } ///:~
  79.